import {
  getLineCodeListNew,
  getProcessExceptionAnalysisExport,
} from "@/api/report/mesProdOrderPlanPassRatio.js";
import { getTeamCodeList } from "@/api/employee/mesUserExtend";
import moment from "moment";
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", //点击时返回的标识
    url: getProcessExceptionAnalysisExport,
    //auth: "auth:operlog:export", //权限字段
  },
  // {
  //   name: "删除",
  //   type: "danger",
  //   icon: "el-icon-delete",
  //   handleName: "deleteBatch", //点击时返回的标识
  //   isMultiple: true, // isSingle 表示必须选择一项//isMultiple: true, // 表示必须多选
  // },
  // {
  //   name: "导入",
  //   type: "primary",
  //   icon: "el-icon-circle-plus-outline",
  //   handleName: "import", //点击时返回的标识
  // },
];
// 行末按钮配置
export const rowConfig = [];
export const searchConfig = {
  data() {
    const self = this;
    var choiceDate = null;
    return {
      searchConfig: {
        inline: true,
        formDesc: {
          searchTimeType: {
            type: "radio-button",
            layout: 6,
            labelWidth: "10px",
            options: [
              { text: $i18n.t("common.yesterday"), value: "0" },
              { text: $i18n.t("common.today"), value: "1" },
              { text: $i18n.t("common.thisWeek"), value: "2" },
              {
                text: $i18n.t("mesProductionDailyClear.thisMonth"),
                value: "3",
              },
            ],
            on: {
              change: (val) => {
                self.timeChange(val);
              },
            },
          },
          actualEndDate: {
            type: "datetimerange",
            layout: 8,
            label: $i18n.t("abnormalProcessAnalysis.actualEndDate"),
            attrs: {
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              searchArrKey: ["searchStartTime", "searchEndTime"],
              clearable: false,
              pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                  // 把选择的第一个日期赋值给一个变量。
                  choiceDate = minDate.getTime();

                  // 如何你选择了两个日期了，就把那个变量置空
                  if (maxDate) choiceDate = "";
                },
                disabledDate: (time) => {
                  // 如何选择了一个日期
                  if (choiceDate) {
                    // 7天的时间戳
                    const one = 30 * 24 * 3600 * 1000;
                    // 当前日期 - one = 7天之前
                    const minTime = choiceDate - one;
                    // 当前日期 + one = 7天之后
                    const maxTime = choiceDate + one;
                    return time.getTime() < minTime || time.getTime() > maxTime;
                  } else {
                    // 如果没有选择日期，就要限制不能选择今天及以后
                    // return time.getTime() > Date.now();
                  }
                },
              },
            },
          },
          workshopCode: {
            label: $i18n.t("mesProdOrderPlanPassRatio.workshopName"),
            type: "select",
            layout: 5,
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode: self.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await getLineCodeListNew(params);
              return list.data.result;
            },
          },
          lineCode: {
            labelWidth: "90px",
            // layout: 7,
            label: $i18n.t("mesProdOrderPlanPassRatio.lineName"),
            type: "select",
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            optionsLinkageFields: ["workshopCode"],
            options: async (data) => {
              if (data && data.workshopCode) {
                const params = {
                  modelingCode: data.workshopCode,
                  level: 5,
                };
                const list = await getLineCodeListNew(params);
                return list.data.result;
              } else {
                return [];
              }
            },
          },
          workTeamCode: {
            type: "select",
            label: $i18n.t("common.team"),
            optionsLinkageFields: ["lineCode"],
            isReloadOptions: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "teamCode",
              text: "teamName",
            },
            options: async (data) => {
              // const list = await getTeamCodeList();
              // return list.data.result;
              if (data.lineCode) {
                const list = await getTeamCodeList({ lineCode: data.lineCode });
                return list.data.result;
              } else {
                return [];
              }
            },
          },
          processName: {
            // layout: 7,
            labelWidth: "80px",
            type: "input",
            label: $i18n.t("abnormalProcessAnalysis.processName"),
          },
        },
      },
    };
  },
  methods: {
    timeChange(val) {
      let data =   this.$refs.eleTable.getSearchParams()
      if (val == "0") {
        let timer1 = moment().subtract(1, "days").format("YYYY-MM-DD 00:00:00");
        let timer2 = moment().subtract(1, "days").format("YYYY-MM-DD 23:59:59");
        this.initForm = {...data,  searchTimeType: val, actualEndDate: [timer1, timer2] };
        console.log("initForm", this.initForm);
      } else if (val == "1") {
        this.initForm = {...data, 
          searchTimeType: val,
          actualEndDate: [
            moment().format("YYYY-MM-DD 00:00:00"),
            moment().format("YYYY-MM-DD HH:mm:ss"),
          ],
        };
      } else if (val == "2") {
        let startWeek = moment().startOf("isoWeek").format("YYYY-MM-DD 00:00:00");
        let endWeek = moment().endOf("isoWeek").format("YYYY-MM-DD HH:mm:ss");
        this.initForm = {...data, 
          searchTimeType: val,
          actualEndDate: [startWeek, endWeek],
        };
      } else {
        let startMonth = moment().startOf("month").format("YYYY-MM-DD 00:00:00");
        let endMonth = moment().endOf("month").format("YYYY-MM-DD HH:mm:ss");
        this.initForm = {...data, 
          searchTimeType: val,
          actualEndDate: [startMonth, endMonth],
        };
      }
    },
  },
};
